package com.jishuo.base.controller;

import com.jishuo.base.annotation.UserAuthority;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.bean.Pagination;
import com.jishuo.base.bean.vo.RoleVO;
import com.jishuo.base.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色管理
 */
@Slf4j
@Controller
@RequestMapping(value = "/sys/role", name = "角色管理")
@UserAuthority(menu = "sys/role/index")
public class RoleController {
    @Resource
    private RoleService roleService;

    // 页面文件位置
    private String prefix = "sys/role/";

    @RequestMapping(value = {"index"}, name = "首页")
    public String index() {
        return prefix + "index";
    }

    @ResponseBody
    @RequestMapping(value = "listRole", name = "角色列表")
    public GridResult<RoleVO> listRole(RoleVO role, Pagination pagination) {
        return new GridResult<>(this.roleService.listRole(role, pagination));
    }

    @ResponseBody
    @RequestMapping(value = "getRole", name = "获得角色")
    public BaseResult getRole(int roleId) {
        RoleVO role = this.roleService.getRole(roleId);
        if (role == null)
            return new BaseResult(ErrorCodeEnum.DATA_NOT_FOUND);
        else
            return new BaseResult(role);
    }

    @ResponseBody
    @RequestMapping(value = "addRole", name = "新增角色")
    public BaseResult addRole(RoleVO role) {
        return this.roleService.addRole(role);
    }

    @ResponseBody
    @RequestMapping(value = "saveRole", name = "更新角色")
    public BaseResult saveRole(RoleVO role) {
        return this.roleService.saveRole(role);
    }

    @ResponseBody
    @RequestMapping(value = "deleteRole", name = "删除角色")
    public BaseResult deleteRole(int roleId) {
        return this.roleService.deleteRole(roleId);
    }

    @ResponseBody
    @RequestMapping(value = "listAllRole", name = "全部角色列表")
    public List<RoleVO> listAllRole() {
        return this.roleService.listRole(new RoleVO(), null);
    }
}
